package com.slipper.sfts.order.service.support;

import com.slipper.PaasContext;
import com.slipper.common.util.StringUtil;
import com.slipper.qmt.vo.OrderErrorVO;
import com.slipper.sfts.api.stock.bo.StockOrderBO;
import com.slipper.sfts.order.service.IDealer;
import com.slipper.sfts.stock.service.StockOrderMessageService;
import com.slipper.sfts.stock.service.StockOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OrderErrorDealer implements IDealer<OrderErrorVO> {
    private static final Logger logger = LoggerFactory.getLogger(OrderErrorDealer.class);

    @Override
    public void doDeal(OrderErrorVO vo) {
        logger.info("接收到委托错误消息：{}", vo);

        if (StringUtil.isBlank(vo.getOrderRemark())) return;

        StockOrderService stockOrderService = PaasContext.getBean(StockOrderService.class);
        StockOrderBO stockOrder = stockOrderService.queryBy(vo.getOrderRemark());
        if (stockOrder != null) {
            stockOrder.setFinished(2);
            stockOrder.setStatus(StockOrderBO.STATUS_JUNK);
            stockOrder.setCancelReason(vo.getErrorMsg());
            stockOrderService.save(stockOrder);

            String message = "[" + stockOrder.getCode() + "]委托失败，" +
                    "失败信息[" + vo.getErrorMsg() + "]";
            PaasContext.getBean(StockOrderMessageService.class).save(
                    stockOrder.getMemberId(), stockOrder.getAccountId(), message);
        }
    }
}
